package my.lang.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.vfs.VirtualFile;

import static my.lang.MyLanguage.LANG_FILE_SUFFIX;

public abstract class MyAction extends AnAction {

    @Override
    public void update(AnActionEvent e) {
        boolean isShow = isScriptCode(e);
        e.getPresentation().setEnabledAndVisible(isShow);
    }

    /**
     * 判断是否脚本后缀
     *
     * @param event
     * @return
     */
    public static boolean isScriptCode(AnActionEvent event) {

        VirtualFile virtualFile = event.getData(CommonDataKeys.VIRTUAL_FILE);
        if (virtualFile == null) {
            return false;
        }
        String filePath = virtualFile.getPath();
        return filePath.endsWith(LANG_FILE_SUFFIX);
    }

}
